<?php
/**
 * Created by PhpStorm.
 * User: asdf
 * Date: 2021/9/21
 * Time: 22:16
 */

namespace core\lib;
use core\lib\APPcheck;
use core\lib\APPloader;
use function core\p;
use core\Waf;

class AppRunner
{
    /*
     * @第二程序中枢 !
     * @参数检测 !
     * @非法请求检测
     * @利用Redis记录非法请求,并阻止ip访问
     * @调用权限 !
     * @反射运行 !
     * @访问日志
     * */
    /*
     * 自动加载APP(模式单应用|多应用)
     * URI检查(非法字符->redis)
     * 路由解析(解析规则配置)
     * 参数检查(参数缺失,缺省参数,参数名错误)   !
     * 反射调用   !
     * */

    private static $instance;

    private static $infomation_src;
    private static $router;
    private static $apploader;
    private static $appchecker;

    private static $ip;
    private static $cookie;
    private static $info_arr = [];

    private static $is_MAPP;
    private static $forder;
    private static $uri;
    private static $ctl;
    private static $action;





    private function __clone()
    {
        // TODO: Implement __clone() method.
    }

    private function __construct()
    {
        $this->loader();
        $this->Run();
    }

    private function loader(){
        self::$infomation_src = Queryinfom::Newinstance();
        self::$ip = self::$infomation_src->ip;
        self::$cookie = self::$infomation_src->cookie;
        self::$is_MAPP = self::$infomation_src->is_MAPP;

        if (self::$is_MAPP){
            self::$uri = self::$infomation_src->MAPPurl();
        }else{
            self::$uri = self::$infomation_src->basuri;
        }


        if (self::$is_MAPP){
            self::$router = MAPProter::Newinstance(self::$uri);
            self::$forder = self::$router->forder;
            self::$ctl = self::$router->ctl;
            self::$action = self::$router->action;
        }else{
            self::$router = route::Newinstance(self::$uri);
            self::$ctl = self::$router->ctl;
            self::$action = self::$router->action;
        }


    }

    public static function Newinstance(){
        if(!self::$instance instanceof self){
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function Run(){

        //加载
        if (self::$is_MAPP){
            APPloader::MAPPloader(self::$forder,self::$ctl);
            $claz = APPloader::MCtlloader(self::$forder,self::$ctl);
            $pamf = self::$infomation_src->MappGetparms();
        }else{
            APPloader::APPloadersingle(self::$ctl);
            $claz = APPloader::Ctlloader(self::$ctl);
            $pamf = self::$infomation_src->Getparms();
        }


        //运行
        // use ioc container
        try{
            $ee = ReflectionTool::Instance($claz);
            $ee->Method(self::$action);
//            p(self::$action);
//            p(self::$ctl);
//            p($ee->getPamenentsNums());
            if ($ee->getPamenentsNums() == 0){
                Container::run($claz,self::$action,[]);
            }else{
                if (($parms = $ee->Fillparms($pamf,self::$action)) == false){
//                    p($parms);
                    Container::run($claz,self::$action,[]);
                }else {
//                    p($parms);
                    Container::run($claz, self::$action, $parms);
                }
            }
        }catch (\Exception $e){
            p("出错了");
            p($e->getMessage());
            die();
        }


    }


}